package com.zmn.oms.business.impl.conf.distribute;


import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightBService;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightPlatBService;
import com.zmn.oms.common.constant.ConfDistributeConsts;
import com.zmn.oms.model.entity.conf.distribute.DistributeWeightCompany;
import com.zmn.oms.model.entity.conf.distribute.DistributeWeightCompanyQuery;
import com.zmn.oms.model.entity.conf.distribute.DistributeWeightPlat;
import com.zmn.oms.model.vo.conf.distribute.DistributeWeightVO;
import com.zmn.oms.services.interfaces.conf.distribute.DistributeWeightCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：派单配置业务服务
 *
 * @author liuying
 * @date 2022/02/16 10:48
 */
@Slf4j
@Service
public class DistributeWeightBServiceImpl implements DistributeWeightBService {

    @Resource
    private DistributeWeightPlatBService distributeWeightPlatBService;

    @Resource
    private DistributeWeightCompanyService distributeWeightCompanyService;

    @Override
    public DistributeWeightVO findDistributeWeightById(Integer plat, Integer companyId, Integer cityId, Integer bizType) {

        DistributeWeightCompanyQuery query = new DistributeWeightCompanyQuery();
        query.setPlat(plat);
        query.setCompanyId(companyId);
        query.setCityId(cityId);
        query.setBizType(bizType);
        List<DistributeWeightCompany> companyConfigList = distributeWeightCompanyService.listPageByQuery(query);
        // 没有公司配置时，取平台配置
        DistributeWeightVO distributeWeight;
        Integer distributeApplication;
        Integer distributeConfigId;
        if (CollectionUtils.isEmpty(companyConfigList)) {
            DistributeWeightPlat configPlat = distributeWeightPlatBService.findServiceProviderConfigByPlatAndBizType(plat, bizType);
            if (Objects.isNull(configPlat)) {
                return null;
            }
            distributeWeight = BeanMapper.map(configPlat, DistributeWeightVO.class);
            distributeApplication = ConfDistributeConsts.DISTRIBUTE_APPLICATION_PLAT;
            distributeConfigId = configPlat.getDistributePlatId();
        } else {
            DistributeWeightCompany compConfig = companyConfigList.get(0);
            // 针对“42989”服务商做特殊处理
            if (42989 == companyId) {
                compConfig.setAutoStatusNew(GlobalConsts.NO);
                compConfig.setAutoStatusRework(GlobalConsts.NO);
                compConfig.setRecommendStatus(GlobalConsts.NO);
            }
            distributeWeight = BeanMapper.map(compConfig, DistributeWeightVO.class);
            distributeApplication = ConfDistributeConsts.DISTRIBUTE_APPLICATION_COMPANY;
            distributeConfigId = compConfig.getWeightId();
        }
        if (distributeWeight != null) {
            distributeWeight.setDistributeApplication(distributeApplication);
            distributeWeight.setDistributeConfigId(distributeConfigId);
        }
        return distributeWeight;

    }
}
